

pub fn convert_base(num_str: &str, to_base: u32) -> String {
    // 进制转换: N(X) -> N(10) -> N(to_base)

    let parts: Vec<&str> = num_str.split("(").collect();
    let radix = match parts[1].trim_end_matches(")").parse::<u32>() {
        Ok(r) if r >= 2 && r <= 16 => r,
        _ => panic!("Invalid radix ...")
    };
    // N(X) -> N(10)
    let num: i32 = i32::from_str_radix(parts[0].trim(), radix).unwrap();
    convert(num, to_base)
}


fn convert(num: i32, to_base: u32) -> String {
    // num(10) -> N(to_base)
    if num == 0 {
        return "0".to_string();
    }
    let mut ans = String::new();
    let negative = num < 0;
    let mut num =  if negative {(-num) as u32} else {num as u32};

    while num > 0 {
        let digit = num % to_base;
        let chr = std::char::from_digit(digit, to_base).unwrap();
        ans.insert(0, chr);
        num /= to_base;
    }

    if negative {ans.insert(0, '-');}

    ans
}